﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using DevExpress.XtraEditors;
using Waker.APIs.ZKT;
using Waker.Entitys;

namespace Waker.Cards
{
    public partial class FormStaffFinger : Base.FormBasePop
    {
        DataTable _DtSource;
        //ZKTHelper zktHelp;
        public FormStaffFinger(string id)
        {
            InitializeComponent();
            _DtSource = new BAL.BAL_Cards().GetEmployeeByID(id);
            this.Load += new EventHandler(FormStaffFinger_Load);
            this.Text += string.Format("[{0}]", _DtSource.Rows[0]["Emp_Name"]);
        }

        void FormStaffFinger_Load(object sender, EventArgs e)
        {
            try
            {
                StartWaiting();
                ControlUtil.BindData(this.Controls, _DtSource);
                //zktHelp = new ZKTHelper(); 
                var res = ZKTHelper.ZKTConnect();
                if (res.IsSuccess)
                    lblFingerStatus.Text = "已经连接";
                else
                    lblFingerStatus.Text = res.Message;
            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
            finally
            {
                EndWaiting();
            }
        }
        private void sbtnInFinger_Click(object sender, EventArgs e)
        {
            try
            {
                var btn = sender as SimpleButton;
                if (btn == null)
                    return;
                var index = Convert.ToInt32(btn.Name.Substring(btn.Name.Length - 1, 1));
                if (index < 0 || index > 9)
                    return;
                if (_DtSource.Rows[0]["FingerId"] == DBNull.Value)
                    return;
                var chks = this.Controls.Find("chkFinger" + index, false);
                if (chks.Length < 1)
                {
                    return;
                }
                var chk = chks[0] as CheckEdit;
                if (chk == null)
                    return;
                chk.Checked = false;
                StartWaiting();
                if (_DtSource.Rows[0]["FingerId"] == DBNull.Value || Convert.ToInt32(_DtSource.Rows[0]["FingerId"]) < 1)
                {
                    return;
                }
                var fingId = Convert.ToInt32(_DtSource.Rows[0]["FingerId"]);
                //string name = string.Empty, pwd = string.Empty;
                //int privilege = -1;
                //bool enable = false;
                //var a = ZKTHelper.ZKTGetUserInfo(fingId, ref name, ref pwd, ref privilege, ref enable);
                //var b = ZKTHelper.GetUserTmpExStr(fingId, 0, out privilege);

                var bal = new BAL.BAL_Cards();
                var result = ZKTHelper.ZKTStartEnroll(fingId, index,
                    (EnrollNumber, FingerIndex, ActionResult, TemplateLength) =>
                    {
                        if (FingerIndex != index || EnrollNumber != fingId || chk.Checked)
                            return;
                        if (ActionResult == 0)
                        {
                            ZKTHelper.ZKTRefreshData();
                            ZKTHelper.ZKTStartVerify(fingId, FingerIndex);
                            if (!bal.UpdateEmployeeFingerIndex(
                                                _DtSource.Rows[0]["Emp_ID"].ToString(),
                                                index))
                            {
                                UI.ShowError("保存用户指纹登记标记失败,请重试");
                                return;
                            }
                            chk.Checked = true;
                            UI.ShowInfo(chk.Text + "  指纹登记成功");
                        }
                        else
                        {
                            UI.ShowError("指纹登记失败,请重试");
                            return;
                        }
                    }
                );
                if (!result.IsSuccess)
                {
                    UI.ShowError("启动指纹登记失败,请重试");
                    return;
                }

            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
            finally
            {
                EndWaiting();
            }
        }
        private void sbtnCheckFinger_Click(object sender, EventArgs e)
        {
            try
            {
                var btn = sender as SimpleButton;
                if (btn == null)
                    return;
                var index = Convert.ToInt32(btn.Name.Substring(btn.Name.Length - 1, 1));
                if (index < 0 || index > 9)
                    return;
                if (_DtSource.Rows[0]["FingerId"] == DBNull.Value)
                    return;
                var chks = this.Controls.Find("chkFinger" + index, false);
                if (chks.Length < 1)
                {
                    return;
                }
                var chk = chks[0] as CheckEdit;
                if (chk == null)
                    return;
                if (!chk.Checked)
                {
                    UI.ShowError("请先登记指纹");
                    return;
                }
                if (_DtSource.Rows[0]["FingerId"] == DBNull.Value || Convert.ToInt32(_DtSource.Rows[0]["FingerId"]) < 1)
                {
                    return;
                }
                StartWaiting();
                var fingId = Convert.ToInt32(_DtSource.Rows[0]["FingerId"]);
                //string name = string.Empty, pwd = string.Empty;
                //int privilege = -1;
                //bool enable = false;
                //var a = ZKTHelper.ZKTGetUserInfo(fingId, ref name, ref pwd, ref privilege, ref enable);
                //var b = ZKTHelper.GetUserTmpExStr(fingId, 0, out privilege);


                var result = ZKTHelper.ZKTStartIdentify();
                //    (EnrollNumber, FingerIndex, ActionResult, TemplateLength) =>
                //    {
                //        if (ActionResult == 0)
                //        {
                //            ZKTHelper.ZKTRefreshData();
                //            ZKTHelper.ZKTStartVerify(fingId, index);
                //            if (!bal.UpdateEmployeeFingerIndex(
                //                                _DtSource.Rows[0]["Emp_ID"].ToString(),
                //                                index))
                //            {
                //                UI.ShowError("保存用户指纹登记标记失败,请重试");
                //                return;
                //            }
                //            chk.Checked = true;
                //            UI.ShowInfo(chk.Text + "  指纹登记成功");
                //        }
                //        else
                //        {
                //            UI.ShowError("指纹登记失败,请重试");
                //            return;
                //        }
                //    }
                //);
                //if (!result.IsSuccess)
                //{
                //    UI.ShowError("启动指纹登记失败,请重试");
                //    return;
                //}

            }
            catch (Exception ex)
            {
                UI.ShowError(ex.Message);
            }
            finally
            {
                EndWaiting();
            }
        }
        //protected override void FormClose()
        //{
        //    zktHelp = null;
        //    base.FormClose();
        //}
    }
}
